<template>
	<view class="balance-details-page">
		<!-- 自定义头部 -->
		<headerSlot :propsColor="'#fff'">
			<template v-slot:header>
				<view class="header-container" @click="closePage()">
					<view class="back-btn">
						<text class="cuIcon-back"></text>
					</view>
					<view class="page-title">
						佣金中心
					</view>
				</view>
			</template>
		</headerSlot>

		<!-- 主要内容区域 -->
		<view class="content-container">
			<!-- 佣金卡片 -->
			<view class="balance-card">
				<view class="card-header">
					<text class="card-title">可提现佣金</text>
				</view>
				<view class="balance-content">
					<view class="main-balance">
						<text class="currency">￥</text>
						<text class="amount">{{formatAmount(incomeInfoData.withdrawableCommission)}}</text>
					</view>
					<view class="income-stats">
						<view class="stat-item">
							<view class="stat-label">今日收入</view>
							<view class="stat-value">￥{{formatAmount(incomeInfoData.todayOrderIncome)}}</view>
						</view>
						<view class="stat-item">
							<view class="stat-label">总收入</view>
							<view class="stat-value">￥{{formatAmount(incomeInfoData.totalOrderIncome)}}</view>
						</view>
					</view>
				</view>
			</view>

			<!-- 功能菜单 -->
			<view class="menu-section">
				<view class="menu-item" v-for="(item, index) in balancePagesList" :key="index"
					@click="goPage(item.url)">
					<view class="menu-content">
						<view class="menu-icon">
							<text :class="item.icon"></text>
						</view>
						<view class="menu-text">{{item.label}}</view>
						<view class="menu-arrow">
							<text class="cuIcon-right"></text>
						</view>
					</view>
				</view>
			</view>

			<!-- 加载状态 -->
			<view v-if="loading" class="loading-container">
				<uni-load-more status="loading" :content-text="loadingText"></uni-load-more>
			</view>
		</view>
	</view>
</template>

<script>
	import headerSlot from '@/components/header-slot.vue';
	import Storage from '@/utils/storage.js';
	import {
		incomeInfo
	} from "@/api/mine.js"

	export default {
		components: {
			headerSlot,
		},
		data() {
			return {
				loading: false,
				incomeInfoData: {},
				balancePagesList: [{
						label: '佣金明细',
						url: '/pages/mePages/balance/details',
						icon: 'cuIcon-form'
					},
					{
						label: '佣金提现',
						url: '/pages/mePages/balance/Withdrawal',
						icon: 'cuIcon-refund'
					},
					{
						label: '提现记录',
						url: '/pages/mePages/balance/WithdrawRecords',
						icon: 'cuIcon-list'
					}
				],
				loadingText: {
					contentdown: '上拉显示更多',
					contentrefresh: '正在加载...',
					contentnomore: '没有更多数据了'
				}
			};
		},
		onLoad() {
			this.init()
		},
		onPullDownRefresh() {
			this.init()
		},
		methods: {
			async init() {
				try {
					this.loading = true
					const res = await incomeInfo()
					this.incomeInfoData = res.data.data || {}
				} catch (error) {
					console.error('获取收入信息失败:', error)
					uni.showToast({
						title: '获取数据失败',
						icon: 'none'
					})
				} finally {
					this.loading = false
					uni.stopPullDownRefresh()
				}
			},
			closePage() {
				uni.navigateBack()
			},
			goPage(route) {
				if (route == '/pages/mePages/balance/Withdrawal') {
					// 检查业主认证状态
					const userInfo = Storage.getUserInfo()
					if (!userInfo || !userInfo.isOwner) {
						// 显示认证提示弹窗
						uni.showModal({
							title: '需要业主认证',
							content: '只有认证业主才能进行佣金提现，请先完成业主认证',
							confirmText: '去认证',
							cancelText: '取消',
							success: (res) => {
								if (res.confirm) {
									// 跳转到业主认证页面
									this.$router.smartNavigate({
										url: '/pages/mePages/certifiedOwners'
									})
								}
							}
						})
						return
					}

					route = `/pages/mePages/balance/Withdrawal?balance=${this.incomeInfoData.withdrawableCommission}`
				}
				this.$router.smartNavigate({
					url: route
				})
			},
			formatAmount(amount) {
				if (!amount && amount !== 0) return '0.00'
				return Number(amount).toFixed(2)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.balance-details-page {
		min-height: 100vh;
		background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
	}

	.header-container {
		display: flex;
		align-items: center;
		padding: 0 32rpx;
		width: 100%;
		height: 88rpx;

		.back-btn {
			margin-right: 20rpx;
			padding: 8rpx;

			.cuIcon-back {
				font-size: 36rpx;
				color: #2C2622;
			}
		}

		.page-title {
			font-weight: 600;
			font-size: 36rpx;
			color: #2C2622;
		}
	}

	.content-container {
		padding: 32rpx;
	}

	.balance-card {
		background: linear-gradient(135deg, #FE6100 0%, #FF6B35 100%);
		border-radius: 24rpx;
		padding: 40rpx 32rpx;
		margin-bottom: 32rpx;
		box-shadow: 0 8rpx 32rpx rgba(254, 97, 0, 0.3);

		.card-header {
			margin-bottom: 32rpx;

			.card-title {
				font-size: 28rpx;
				color: rgba(255, 255, 255, 0.9);
				font-weight: 500;
			}
		}

		.balance-content {
			.main-balance {
				display: flex;
				align-items: baseline;
				margin-bottom: 40rpx;

				.currency {
					font-size: 32rpx;
					color: rgba(255, 255, 255, 0.9);
					font-weight: 500;
					margin-right: 8rpx;
				}

				.amount {
					font-size: 64rpx;
					color: #FFFFFF;
					font-weight: 700;
					line-height: 1;
				}
			}

			.income-stats {
				display: flex;
				justify-content: space-between;

				.stat-item {
					flex: 1;
					text-align: center;

					.stat-label {
						font-size: 24rpx;
						color: rgba(255, 255, 255, 0.8);
						margin-bottom: 8rpx;
					}

					.stat-value {
						font-size: 32rpx;
						color: #FFFFFF;
						font-weight: 600;
					}
				}
			}
		}
	}

	.menu-section {
		background: #FFFFFF;
		border-radius: 20rpx;
		overflow: hidden;
		box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);

		.menu-item {
			&:not(:last-child) {
				border-bottom: 1rpx solid #f5f5f5;
			}

			.menu-content {
				display: flex;
				align-items: center;
				padding: 32rpx 24rpx;
				transition: background-color 0.2s;

				&:active {
					background-color: #f8f9fa;
				}

				.menu-icon {
					margin-right: 24rpx;
					width: 48rpx;
					height: 48rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					background: linear-gradient(135deg, #FE6100 0%, #FF6B35 100%);
					border-radius: 12rpx;

					text {
						font-size: 24rpx;
						color: #FFFFFF;
					}
				}

				.menu-text {
					flex: 1;
					font-size: 32rpx;
					color: #2C2622;
					font-weight: 500;
				}

				.menu-arrow {
					.cuIcon-right {
						font-size: 28rpx;
						color: #8F97A2;
					}
				}
			}
		}
	}

	.loading-container {
		margin-top: 32rpx;
		text-align: center;
	}
</style>